<?php
class Lesson extends CI_Model 
{
	function __construct()
    {
        parent::__construct();
    }
    public function get_lesson_info($lesson_id)
    {
        $vars = array($lesson_id);
        $query = $this->db->query('SELECT * FROM lessons WHERE id = ? LIMIT 1', $vars);
        if($query->num_rows() == 0)
        {
            return false;
        }
        else
        {
            return $query->row();
        }

    }
    public function get_lesson_parts($lesson_id)
    {
        $vars = array($lesson_id);
        $query = $this->db->query('SELECT * FROM lesson_part WHERE lesson_id = ? ORDER BY order_in_lesson', $vars);
        if($query->num_rows() == 0)
        {
            return false;
        }
        else
        {
            return $query->result();
        }
    }
    public function user_can_edit($lesson_id, $user_id)
    {
        $vars = array($lesson_id, $user_id);
        $query = $this->db->query('SELECT * FROM lessons_admins WHERE lesson_id = ?  AND user_id = ?', $vars);
        if($query->num_rows() > 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    public function save_part_info($part_id, $name, $youtube_link, $youtube_start, $youtube_end)
    {
            $data = array(
               'youtube_link' => $youtube_link,
               'name' => $name,
               'youtube_start' => $youtube_start,
               'youtube_end' => $youtube_end
            );

        $this->db->where('id', $part_id);
        $this->db->update('lesson_part', $data); 
    }
}